﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json.Linq;

namespace xbmc_json_async.System
{
    public class XError
    {
        private JObject mJObject;

        /// <summary>
        /// Gets the internal property object.
        /// </summary>
        public JObject InternalPropertyObject
        {
            get
            {
                return mJObject;
            }
        }

        public int Code;
        public string Method;
        public string Message;
        public string StackMessage;
        public string StackItemName;
        public string StackPropertyMessage;

        //base constructor
        public XError(){}
        public XError(int code, string method,string message, string stackmessage, string stackitemname, string stackpropertymessage, JObject propObject)
        {
            Code = code;
            Method = method;
            Message = message;
            StackMessage = stackmessage;
            StackItemName = stackitemname;
            StackPropertyMessage = stackpropertymessage;
            mJObject = propObject;
        }
        public static XError FromJson(JObject error)
        {
            int code = Convert.ToInt32(error["code"].Value<JValue>().Value.ToString());
            string method = (string)error["data"]["method"];
            string rootMessage = (error["data"]["message"] != null) ? (string)error["data"]["message"] : "";
            string stackmessage = string.Empty;
            string stackitemname = string.Empty;
            string stackpropertymessage = string.Empty;

            if (error["data"]["stack"] != null)
            {
                stackmessage = (error["data"]["stack"]["message"] != null) ? (string)error["data"]["stack"]["message"] : "";
                stackitemname = (error["data"]["stack"]["name"] != null) ? (string)error["data"]["stack"]["name"] : "";
                //stackpropertymessage = (error["data"]["stack"]["property"]["message"] != null) ? (string)error["data"]["stack"]["property"]["message"] : "";
            }


            var errorObject = new XError(
                code,
                method,
                rootMessage,
                stackmessage,
                stackitemname,
                stackpropertymessage,
                error
            );


            return errorObject;
        }

        /*
         "type": "object",
  "properties": {
    "code": { "type": "integer", "required": true },
    "message": { "type": "string", "required": true },
    "data": { "type": "object",
      "properties": {
        "method": { "type": "string", "required": true },
        "stack": { "type": "object", "id": "Error.Stack",
          "properties": {
            "name": { "type": "string", "required": true },
            "type": { "type": "string", "required": true },
            "message": { "type": "string", "required": true },
            "property": { "$ref": "Error.Stack" }
          }
        }
      }
    }
  }
         
         */
    }
}
